package com.chainshop.procurement.service.impl;

import java.util.List;

import com.chainshop.common.core.domain.pojo.ItemInformation;
import com.chainshop.common.utils.DateUtils;
import com.chainshop.common.utils.SecurityUtils;
import com.chainshop.procurement.domain.GoodsTree;
import com.chainshop.procurement.utils.OrderNumberGenerator;
import com.chainshop.product.domain.ItemCategory;
import com.chainshop.product.domain.ProductVariants;
import com.chainshop.product.mapper.ItemCategoryMapper;
import com.chainshop.product.mapper.ItemInformationMapper;
import com.chainshop.product.mapper.ProductVariantsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.chainshop.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.chainshop.procurement.domain.ProcurementItems;
import com.chainshop.procurement.mapper.ProcurementInfoListMapper;
import com.chainshop.procurement.domain.ProcurementInfoList;
import com.chainshop.procurement.service.IProcurementInfoListService;

/**
 * 存储采购信息清单的Service业务层处理
 * 
 * @author zzd
 * @date 2024-08-14
 */
@Service
public class ProcurementInfoListServiceImpl implements IProcurementInfoListService 
{
    @Autowired
    private ProcurementInfoListMapper procurementInfoListMapper;
    @Autowired
    private ItemCategoryMapper itemCategoryMapper;
    @Autowired
    private ItemInformationMapper itemInformationMapper;
    @Autowired
    private ProductVariantsMapper productVariantsMapper;

    /**
     * 查询存储采购信息清单的
     * 
     * @param listId 存储采购信息清单的主键
     * @return 存储采购信息清单的
     */
    @Override
    public ProcurementInfoList selectProcurementInfoListByListId(String listId)
    {
        return procurementInfoListMapper.selectProcurementInfoListByListId(listId);
    }

    /**
     * 查询存储采购信息清单的列表
     * 
     * @param procurementInfoList 存储采购信息清单的
     * @return 存储采购信息清单的
     */
    @Override
    public List<ProcurementInfoList> selectProcurementInfoListList(ProcurementInfoList procurementInfoList)
    {
        return procurementInfoListMapper.selectProcurementInfoListList(procurementInfoList);
    }

    /**
     * 新增存储采购信息清单的
     * 
     * @param procurementInfoList 存储采购信息清单的
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProcurementInfoList(ProcurementInfoList procurementInfoList)
    {
        procurementInfoList.setListId(OrderNumberGenerator.generateOrderNumber());
        procurementInfoList.setAdminName(SecurityUtils.getUsername());
        procurementInfoList.setApplicationTime(DateUtils.getNowDate());
        int rows = procurementInfoListMapper.insertProcurementInfoList(procurementInfoList);
        insertProcurementItems(procurementInfoList);
        return rows;
    }

    /**
     * 修改存储采购信息清单的
     * 
     * @param procurementInfoList 存储采购信息清单的
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProcurementInfoList(ProcurementInfoList procurementInfoList)
    {
        procurementInfoListMapper.deleteProcurementItemsByPurchaseListId(procurementInfoList.getListId());
        insertProcurementItems(procurementInfoList);
        return procurementInfoListMapper.updateProcurementInfoList(procurementInfoList);
    }

    /**
     * 批量删除存储采购信息清单的
     * 
     * @param listIds 需要删除的存储采购信息清单的主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProcurementInfoListByListIds(String[] listIds)
    {
        procurementInfoListMapper.deleteProcurementItemsByPurchaseListIds(listIds);
        return procurementInfoListMapper.deleteProcurementInfoListByListIds(listIds);
    }

    /**
     * 删除存储采购信息清单的信息
     * 
     * @param listId 存储采购信息清单的主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProcurementInfoListByListId(String listId)
    {
        procurementInfoListMapper.deleteProcurementItemsByPurchaseListId(listId);
        return procurementInfoListMapper.deleteProcurementInfoListByListId(listId);
    }

    @Override
    public List<GoodsTree> getTree() {
        List<GoodsTree> goodsTreeList=new ArrayList<>();
        List<ItemCategory> itemCategories = itemCategoryMapper.selectItemCategoryList(new ItemCategory());
        for (ItemCategory itemCategory : itemCategories) {
            GoodsTree goodsTree = new GoodsTree(itemCategory.getCategory(), itemCategory.getCategory(),null);
            List<GoodsTree> children=new ArrayList<>();;

            ItemInformation itemInformation = new ItemInformation();
            itemInformation.setCategoryId(itemCategory.getId());
            List<ItemInformation> itemInformations = itemInformationMapper.selectItemInformationList(itemInformation);
            //ddddddddddddddd
            System.out.println(itemInformations);

            if(!itemInformations.isEmpty()){
                for (ItemInformation itemInformation1 : itemInformations) {
                    GoodsTree goodsTree1 = new GoodsTree(itemInformation1.getProductName(), itemInformation1.getProductName(),null);
                    List<GoodsTree> children1=new ArrayList<>();
                    ProductVariants productVariants1 = new ProductVariants();
                    productVariants1.setProductId(itemInformation1.getId());
                    //dddddddddddd
                    List<ProductVariants> productVariants = productVariantsMapper.selectProductVariantsList(productVariants1);
                    System.out.println(productVariants);

                    if(!productVariants.isEmpty()){
                        for (ProductVariants productVariants2 : productVariants) {
                            GoodsTree goodsTree2 = new GoodsTree("商品变种id:"+productVariants2.getVariantId()+"  "+productVariants2.getGoodsAttribute(), "" + productVariants2.getVariantId(), null);
                            children1.add(goodsTree2);
                        }
                    }
                    goodsTree1.setChildren(children1);
                    children.add(goodsTree1);
                }
            }
            goodsTree.setChildren(children);
            goodsTreeList.add(goodsTree);



        }
//        System.out.println(goodsTreeList);
        return goodsTreeList;
    }

    @Override
    public ProductVariants getVariantsProduct(String vid) {

        return productVariantsMapper.selectProductVariantsByVariantId(Long.parseLong(vid));
    }

    /**
     * 新增存储采购清单中的商品信息信息
     * 
     * @param procurementInfoList 存储采购信息清单的对象
     */
    public void insertProcurementItems(ProcurementInfoList procurementInfoList)
    {
        List<ProcurementItems> procurementItemsList = procurementInfoList.getProcurementItemsList();
        String listId = procurementInfoList.getListId();
        if (StringUtils.isNotNull(procurementItemsList))
        {
            List<ProcurementItems> list = new ArrayList<ProcurementItems>();
            for (ProcurementItems procurementItems : procurementItemsList)
            {
                procurementItems.setPurchaseListId(listId);
                list.add(procurementItems);
            }
            if (list.size() > 0)
            {
                procurementInfoListMapper.batchProcurementItems(list);
            }
        }
    }
}
